const userinfo = require("../database/userinfo");
const verify = require("../modules/verify");

module.exports = async (req, res)=>{
  const {token} = req.query;

  if(!token){
    res.send({
      code:0,
      msg:"缺少必传参数：token",
      data:null
    })
    return;
  }

  try{
    const tokenData = await verify(token);
    const userData = await userinfo.find({ userId: tokenData.id });
    res.send({
      code: 1,
      msg: "获取个人信息成功",
      data: userData[0]
    })
  }catch(err){
    if(err.name === "JsonWebTokenError"){
      res.send({
        code: 3,
        msg: "登录失效或未登录",
        data: null
      })
    }else{
      res.send({
        code: 2,
        msg: "数据库出错",
        data: err.toString()
      })
    }
  }
}